/**
 * HTTP 请求 axios 封装
 */
import axios from 'axios';
import global from './config'
import process from "shelljs";

// Axios 全局配置
let instance = axios.create();
instance.defaults.baseURL = process.env.VUE_APP_BASE_URL;
instance.defaults.withCredentials = true;

instance.open = function (url, data, target) {
  let form = document.createElement("form");
  if (url.indexOf('localhost:9685') === -1) {
    // 不存在 global.baseURL
    form.action = 'localhost:9685' + url;
  } else {
    form.action = url
  }
  form.method = 'post';
  form.target = target || "_self";
  if (data) {
    for (let key in data) {
      let input = document.createElement("textarea");
      input.name = key;
      input.value = typeof data[key] === "object" ? JSON.stringify(data[key]) : data[key];
      form.appendChild(input);
    }
  }
  form.style.display = 'none';
  document.body.appendChild(form);
  form.submit();
};

export const http = instance;

